home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / OOPTUT34.ZIP / VMT.TXT < prev    next >
Text File  |  1993-06-12  |  4KB  |  98 lines

  1.  
  2.            VIRTUAL METHODS AND THE VIRTUAL METHOD TABLE. 
  3.            ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  4.  
  5.  Every object type, that contains or inherits a virtual method, has a
  6.  Virtual Method Table (VMT) in the data segment.  The VMT contains the size
  7.  of the object type and a set of pointers to the code for each of its
  8.  virtual methods. 
  9.  
  10.  For each instance of an object type, the CONSTRUCTOR establishes a link
  11.  between that instance and the VMT for that object type. This link is a
  12.  16-bit field, called the virtual method table field, which follows the
  13.  ordinary data fields in the object type. 
  14.  
  15.  There is only one VMT for each object type.  Individual instances of an
  16.  object type contain a link to the VMT - they do not contain the VMT itself. 
  17.  
  18.  Example: 
  19.  
  20.  type
  21.    Location = object
  22.      X, Y: Integer;
  23.      procedure Init(NewX, NewY: Integer);
  24.      function GetX: Integer;
  25.      function GetY: Integer;
  26.    end;
  27.  
  28.    PointPtr = ^Point;
  29.    Point = object(Location);
  30.      Color: Integer;
  31.      constructor Init(NewX, NewY, NewColor: Integer);
  32.      destructor Done; virtual;
  33.      procedure Show; virtual;
  34.      procedure Hide; virtual;
  35.      procedure MoveTo(NewX, NewY: Integer); virtual;
  36.    end;
  37.  
  38.    CirclePtr = ^Circle;
  39.    Circle = object(Point);
  40.      Radius: Integer;
  41.      constructor Init(NewX, NewY, NewColor, NewRadius: Integer);
  42.      procedure Show; virtual;
  43.      procedure Hide; virtual;
  44.      procedure Fill: virtual;
  45.    end;
  46.  
  47.  
  48.  Instances of types Location, Point and Circle are shown below:
  49.  
  50.         Location                Point                Circle 
  51.  
  52.     -----------------      -----------------     ----------------- 
  53.     | X             |      | X             |     | X             | 
  54.     -----------------      -----------------     ----------------- 
  55.     | Y             |      | Y             |     | Y             | 
  56.     -----------------      -----------------     ----------------- 
  57.                            | Color         |     | Color         | 
  58.                            -----------------     ----------------- 
  59.                            | VMT           |     | VMT           | 
  60.                            -----------------     ----------------- 
  61.                                                  | Radius        | 
  62.                                                  ----------------- 
  63.  
  64.  Because Point is the first type in the hierarchy that introduces virtual
  65.  methods, the VMT field is allocated immediately after the Color field.
  66.  
  67.  VMTs are built automatically by the compiler and are never directly
  68.  manipulated by a program.  Likewise, pointers to VMTs are automatically
  69.  stored in object type instances by the object type's constructors and are
  70.  never directly manipulated by the program. 
  71.  
  72.  The layouts of the VMTs for point and circle types are shown below: 
  73.  
  74.          Point VMT                     Circle VMT 
  75.     --------------------       ---------------------  ---------------------
  76.     | $0008            |       | $000A             |     Size
  77.     --------------------       ---------------------  ---------------------
  78.     | $FFF8            |       | $FFF6             |  Negative size (check)
  79.     --------------------       ---------------------  ---------------------
  80.     | @Point.Done      |       | @Point.Done       |  Seg:Ofs addresses of
  81.     --------------------       ---------------------
  82.     | @Point.Show      |       | @Circle.Show      |  methods, inherited
  83.     --------------------       ---------------------
  84.     | @Point.Hide      |       | @Circle.Hide      |  like Done and MoveTo
  85.     --------------------       ---------------------
  86.     | @Point.MoveTo    |       | @Point.MoveTo     |  or overridden like
  87.     --------------------       ---------------------
  88.                                | @Circle.Fill      |  Show and Hide.
  89.                                ---------------------  ---------------------
  90.  
  91.  Circle inherits Done and MoveTo methods from Point, but overrides Show and
  92.  Hide methods and has its own new method Fill.
  93.  
  94.  
  95.  VMT.TXT 
  96.  22.8.92 
  97.  1.6.93
  98.